﻿using System;
using System.Globalization;
using System.Windows;

namespace Gmantis.Controls
{
    public class DropDown : DropDownButton
    {
        // Fields
        internal bool _isLoaded;
        public static readonly DependencyProperty DisabledCuesVisibilityProperty = DependencyProperty.Register("DisabledCuesVisibility", typeof(Visibility), typeof(DropDown), new PropertyMetadata(Visibility.Visible));
        public static readonly DependencyProperty FocusCuesVisibilityProperty = DependencyProperty.Register("FocusCuesVisibility", typeof(Visibility), typeof(DropDown), new PropertyMetadata(Visibility.Visible));
        public static readonly DependencyProperty ShowButtonProperty = DependencyProperty.Register("ShowButton", typeof(bool), typeof(DropDown), new PropertyMetadata(true, new PropertyChangedCallback(DropDown.OnShowButtonPropertyChanged)));

        // Methods
        public DropDown()
        {
            base.DefaultStyleKey = typeof(DropDown);
        }

        private void OnAfterApplyTemplate()
        {
            this.UpdateShowButton();
        }

        public override void OnApplyTemplate()
        {
            string str = string.Empty;
            base.OnApplyTemplate();
            this._isLoaded = true;
            if (!string.IsNullOrEmpty(str))
            {
                this._isLoaded = false;
                if ((base.Template != null) && !PlatformIndependent.IsInDesignMode(this))
                {
                    throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Template cannot be applied to DropDown.\nDetails: {0}", new object[] { str }));
                }
            }
            else
            {
                this.OnAfterApplyTemplate();
            }
        }

        private void OnShowButtonChanged(bool oldValue)
        {
            this.UpdateShowButton();
        }

        private static void OnShowButtonPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            DropDown down = d as DropDown;
            bool oldValue = (bool)e.OldValue;
            down.OnShowButtonChanged(oldValue);
        }

        private void UpdateShowButton()
        {
            if (base._elementArrowToggle != null)
            {
                base._elementArrowToggle.Visibility = this.ShowButton ? Visibility.Visible : Visibility.Collapsed;
            }
        }

        // Properties
        public Visibility DisabledCuesVisibility
        {
            get { return (Visibility)GetValue(DisabledCuesVisibilityProperty); }
            set { SetValue(DisabledCuesVisibilityProperty, value); }
        }

        public Visibility FocusCuesVisibility
        {
            get { return (Visibility)GetValue(FocusCuesVisibilityProperty); }
            set { SetValue(FocusCuesVisibilityProperty, value); }
        }

        public bool ShowButton
        {
            get { return (bool)GetValue(ShowButtonProperty); }
            set { SetValue(ShowButtonProperty, value); }
        }
    }
}
